package com.job.project.rabbitmq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import static org.springframework.amqp.core.ExchangeTypes.FANOUT;

/**
 * @Author 王博
 * @DateTime 2024-08-14 16:48
 */
@Slf4j
@Component
public class MessageHandler {
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "myQueue", durable = "true"),
                    exchange = @Exchange(name = "myExchange", type = FANOUT))
            },
            concurrency = "10"
    )
    @RabbitHandler
    public void onMessage(@Payload String mes, Message message, Channel channel)  throws Exception{
        log.info("接收到消息：{}", mes);
        Thread.sleep(10000);
        log.info("消息处理完毕:{}",mes);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
